# -*- coding: utf-8 -*-
import pprint
import urllib,urllib2,cookielib
import re,time,random


def StripExtra(mystr):
    mystr = mystr.replace("font","")
    mystr = mystr.replace("FF","")
    mystr = mystr.replace("00","")
    mystr = mystr.replace("#","")
    mystr = mystr.replace("#","")
    mystr = mystr.replace("<","")
    mystr = mystr.replace(">","")
    mystr = mystr.replace("color","")
    mystr = mystr.replace('"','')  
    mystr = mystr.replace('=','')
    mystr = mystr.replace(' ','')
    mystr = mystr.replace('/','')                
    mystr = mystr.strip()
    return mystr

def GetQueryResult(billcode):
    '''这个就是登录脚本'''

    #登录需要准备cookie
    cookie=cookielib.CookieJar()
    opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cookie), urllib2.HTTPHandler)
    urllib2.install_opener(opener)
 
    #好的，现在填表
    postdata=urllib.urlencode({'ID':billcode,
                           'Submit':'登  陆'
    })
    req = urllib2.Request(
        url = 'http://218.246.21.45/query.asp',
        data = postdata
    )
 
    #填header，伪装成正常浏览访问，这是一种写法，也可以用我之前那篇文章的写法，更有条理一点
    req.add_header('User-Agent','Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.1.9) Gecko/20100315 Firefox/3.5.9 ( .NET CLR 3.5.30729)')
    req.add_header('Accept','text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8')
    req.add_header('Accept-Encoding','gzip,deflate')
    req.add_header('Accept-Charset','ISO-8859-1,utf-8;q=0.7,*;q=0.7') 
    
    #交登录表，然后就登录成功了
    result = urllib2.urlopen(req).read()
    result = result.decode('gbk').encode('utf-8')
    result = result.replace('\r\n','')
#    print result
    
    accept_time_str_0_date_list = re.compile(r'height="22"\>(\d{4}-\d{2}-\d{2})\</td\>').findall(result)
    if(len(accept_time_str_0_date_list) == 0):
        return [False,None,None,None,None,None,None]
    
    accept_time_str_0_date = re.compile(r'height="22"\>(\d{4}-\d{2}-\d{2})\</td\>').findall(result)[0]
    accept_time_str_0_time = re.compile(r'height="22"\>(\d{2}:\d{2})</td\>').findall(result)[0]
        
    accept_time_str_0 =  accept_time_str_0_date + accept_time_str_0_time
    
    accept_time_str_1_date = re.compile(r'height="22"\>(\d{4}-\d{2}-\d{2})\</td\>').findall(result)[0]
    accept_time_str_1_time = re.compile(r'height="22"\>(\d{2}:\d{2})</td\>').findall(result)[-1]
    accept_time_str_1 =  accept_time_str_1_date + accept_time_str_1_time
            
    accept_address_str_0 = re.compile(r'height="22"\>.*?<font color="#FF0000"\>(.*?)</font\>').findall(result)[0]
    accept_address_str_1 = re.compile(r'height="22"\>.*?<font color="#FF0000"\>(.*?)</font\>').findall(result)[-1]
    
    accept_address_str_0 = accept_address_str_0.strip()
    accept_address_str_1 = accept_address_str_1.strip()

    remark_str_0 = re.compile(r'%s(.*?)\</td\>'%(accept_address_str_0)).findall(result)[0]
    remark_str_0 = StripExtra(remark_str_0)
    
    remark_str_1 = re.compile(r'%s(.*?)\</td\>'%(accept_address_str_1)).findall(result)[-1]
    remark_str_1 = StripExtra(remark_str_1)

    return [True,accept_time_str_0,accept_time_str_1,accept_address_str_0,accept_address_str_1,remark_str_0,remark_str_1]

GetQueryResult('1010020989')
GetQueryResult('1010020420')
GetQueryResult('1010105070')
GetQueryResult('1010105994')


